package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Integral;
import cn.wolfcode.trip.base.domain.UserGoods;
import cn.wolfcode.trip.base.service.IUserGoodsService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("usergoods")
public class UserGoodsController {
    @Autowired
    private IUserGoodsService userGoodsService;


    //查询已购买的物品
    @GetMapping("/store/{user.id}")
    public List<UserGoods> listBuySotre(UserGoods userGoods){
        return userGoodsService.selectBuyStore(userGoods);
    }

    //加入购物车
    @PostMapping("{user.id}/store/{store.id}")
    public JsonResult insert(UserGoods userGoods){
        userGoodsService.insert(userGoods);
        return new JsonResult();
    }

    //查出购物车里面的物品
    @GetMapping("{user.id}")
    public List<UserGoods> list(UserGoods userGoods){
        return userGoodsService.listByUserId(userGoods);
    }

    //撤销
    @DeleteMapping("{id}")
    public JsonResult delete(UserGoods userGoods){
        userGoodsService.delete(userGoods);
        return new JsonResult();
    }

    //结算
    @PutMapping("{user.id}")
    public JsonResult put(UserGoods userGoods, int integral){
        JsonResult jsonResult=new JsonResult();
        try {
            userGoods.setBuyTime(new Date());
            userGoodsService.buyStore(userGoods,integral);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }


    @GetMapping("password")
    public JsonResult checkPassword(String password,Long uid){
        JsonResult jsonResult=new JsonResult();
        try{
            userGoodsService.checkPassword(password,uid);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }
}
